package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.BaseCompanyService;
import com.cloudlead.commonData.service.BaseFleetService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseFleet;
import com.cloudlead.fleetAction.service.BaseFleetEditSer;
import com.cloudlead.fleetService.vo.BaseFleetVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jinnoo on 2015/8/12.
 */
@Service
public class BaseFleetEditSerImpl implements BaseFleetEditSer {

    @Autowired
    BaseFleetService baseFleetService;
    @Autowired
    BaseCompanyService baseCompanyService;


    @Override
    public void createFleet(BaseFleetVo vo) {
        Page<BaseFleetVo> p=findAllFleet(vo,1,1);
        if (p.getContent().size()>0){
            throw new RestClientException("车队\""+vo.getFleetName()+"\"已存在。");
        }
        BaseFleet bf = new BaseFleet();
        BeanUtils.copyProperties(vo, bf);
        if(vo.getBaseCompany()!=null){//当公司不为空
            BaseCompany bc = new BaseCompany();
            bc.setId(vo.getBaseCompany());
            bf.setCompany(baseCompanyService.view(vo.getBaseCompany()).getCompanyName());
            bf.setBaseCompany(bc);
        }
        bf.setStatus("1");
        bf.setFlag("0");
        baseFleetService.create(bf);
    }

    @Override
    public void editFleet(BaseFleetVo vo) {
        BaseFleetVo qryVo=new BaseFleetVo();
        qryVo.setBaseCompany(vo.getBaseCompany());
        qryVo.setFleetName(vo.getFleetName());
        Page p=findAllFleet(qryVo,1,1);
        if (p.getContent().size()>0 && !((BaseFleet)p.getContent().get(0)).getId().equals(vo.getId())){
            throw new RestClientException("车队\""+vo.getFleetName()+"\"已存在。");
        }
        BaseFleet bf =baseFleetService.view(vo.getId());
        BeanUtils.copyProperties(vo, bf);
       if(vo.getBaseCompany()!=null){//当公司不为空
            BaseCompany bc = new BaseCompany();
            bc.setId(vo.getBaseCompany());
            bf.setCompany(baseCompanyService.view(vo.getBaseCompany()).getCompanyName());
            bf.setBaseCompany(bc);
        }
        bf.setFlag("0");
        baseFleetService.update(bf);
    }

    @Override
    public Object findOneFleet(BaseFleetVo vo) {
        BaseFleet bf = new BaseFleet();
        BeanUtils.copyProperties(vo, bf);
        BaseFleet bf1=baseFleetService.findOne(bf);
        return bf1 ;
    }

    @Override
    public Page<BaseFleetVo> findAllFleet(BaseFleetVo vo,int page,int size) {
        BaseFleetVo bf1=new BaseFleetVo();
        if(vo!=null) bf1=vo;
        //Filter filter=new Filter(new Criteria(new Term("username", Op,"zhangsan"),new Term("password","zhangsan")),new Criteria(new Term("username","lisi")));
       // baseFleetService.list(filter, null);
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();

        tr.add(new Term("flag","0"));//未删除
        if(vo.getId()!=null&&(!vo.getId().equals("")))//id
            tr.add( new Term("id", vo.getId()));
        if(vo.getBaseCompany()!=null&&(!vo.getBaseCompany().equals("")))//id
            tr.add(new Term("baseCompanyId",vo.getBaseCompany()));
        if(vo.getFleetName()!=null&&(!vo.getFleetName().equals("")))//车队名 模糊查询
            tr.add(new Term("fleetName",Operator.LK,"%"+vo.getFleetName()+"%"));
        if(vo.getAddress()!=null&&(!vo.getAddress().equals("")))//地址 模糊查询
            tr.add(new Term("address",Operator.LK,"%"+ vo.getAddress()+"%"));
        if(vo.getCompany()!=null&&(!vo.getCompany().equals("")))//地址 模糊查询
            tr.add(new Term("company",Operator.LK,"%"+ vo.getCompany()+"%"));
        if(vo.getStatus()!=null&&(!vo.getStatus().equals("")))
            tr.add(new Term("status",vo.getStatus()));//车队类别
        Filter filter=new Filter( new Criteria( tr));
            pr.setPage(page);
            pr.setSize(size);
            pr.setFilter(filter);
        Page<BaseFleet> pb=(Page<BaseFleet>)baseFleetService.findAll(pr);
        Page<BaseFleetVo> pbf=new Page<BaseFleetVo>();
        BeanUtils.copyProperties(pb, pbf);
        return  pbf;
    }

    @Override
    @Transactional//回滚
    public void delFleet(String id) {
        BaseFleet bf= baseFleetService.view(id);
        bf.setFlag("1");//删除
        baseFleetService.update(bf);
    }

    @Override
    public List baseFleetList(String baseCompanyId) {
        BaseFleet baseFleet = new BaseFleet();
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setId(baseCompanyId);
        baseFleet.setBaseCompany(baseCompany);
        List list = baseFleetService.list(baseFleet,null);
        return list;
    }
}
